<?php
/*
---------------------------------------------------------
														+
名称:易通支付通道										+
														+
控制器名称:YiTongController								+
														+
日期:2017-10-11											+
														+
作者:小翁												+
														+
---------------------------------------------------------
*/
namespace Pay\Controller;

use Common\Common\Api\Order;
use Common\Common\Api\Ordermember;
use Common\Common\Api\Redis\Channel;
use Common\Common\Api\Redis\Product;

class Bafang3Controller extends PayController{
    protected $config;
    protected $key;
    protected $url;
    protected $class;
    public function __construct(){
        parent::__construct();
        $classes = explode("\\",__CLASS__);
        $class = end($classes);
        $this->class = substr($class, 0, stripos($class, "Controller"));
    }

    public function pay($array){
        $this->config = Channel::getInstance()->getById($array['channel']);
        if(!$this->config){
            $this->showmessage('通道错误!',['code'=>C("THIRDCODES.".$this->class)]);
        }

        $product = Product::getInstance()->getById(I("request.pay_bankcode",0,"intval"));
        if(!$product){
            $this->showmessage('未找到支付产品!',['code'=>C("THIRDCODES.".$this->class)]);
        }

        $parameter = array(
            'code' => $this->config['code'], // 通道名称
            'title' => $this->config['title'],
            'exchange' => 1, // 金额比例
            'gateway' => $this->config['gateway'],
            'orderid' => createOrderId(),
            'out_trade_id' => I("request.pay_orderid"),
            'body' => 'pay',
            'channel'=>$array
        );

        $return = $this->orderadd( $parameter );

        $pay_memberid = $this->config['mch_id'];   //商户ID
        $pay_orderid = $return['orderid'];
        $pay_applydate = date("Y-m-d H:i:s",time());  //订单时间

        switch($this->config['paytype']){
            //支付宝
            case 3:
            case 4:
                $pay_bankcode = '903';
                break;
            default:
                $this->showmessage('不支持此产品!',['code'=>C("THIRDCODES.".$this->class)]);
        }

        $pay_notifyurl = $return['notifyurl'];
        $pay_callbackurl = $return['callbackurl'];
        $pay_amount = $return['amount'];

        $Md5key = $this->config['signkey'];  //密钥

        $jsapi = array(
            "pay_memberid" => $pay_memberid,
            "pay_orderid" => $pay_orderid,
            "pay_amount" => $pay_amount,
            "pay_applydate" => $pay_applydate,
            "pay_bankcode" => $pay_bankcode,
            "pay_notifyurl" => $pay_notifyurl,
            "pay_callbackurl" => $pay_callbackurl,
        );

        ksort($jsapi);

        $md5str = "";
        foreach ($jsapi as $key => $val) {
            $md5str = $md5str . $key . "=" . $val . "&";
        }

        $sign = strtoupper(md5($md5str . "key=" . $Md5key));


        $jsapi["pay_md5sign"] = $sign;
        $jsapi["pay_attach"] = 'zf';

        po($this->config['gateway'], $jsapi);
    }

    //通知
    public function notifyurl(){
		//$this->debug_callback();
		
        $p = I("post.");
        /*$p = array (
            'returncode' => '00',
            'memberid' => '10046',
            'orderid' => '201808181640527010182',
            'transaction_id' => '20180818164116995197',
            'amount' => '10.00',
            'datetime' => '2018-08-18 16:47:11',
            'sign' => '277A1F76EDD14A0EF3CF56CAD638BDD9',
            'attach' => '原样返回字段',
        );*/

        //获取订单信息
        $orderid = $p['orderid'];
        $find = $this->checkOrderNotify($orderid);

        if(!$find){
            echo 'order_not_found'; exit;
        }
        //保存回调数据
        M("Order")->where(array("pay_orderid"=>$p['busi_order_no']))->save(array(

            "pay_orderid_rd"=>htmlspecialchars($p['transaction_id'])));

        //获取渠道信息
        $this->config = M("Channel")->where(array('code'=>$this->class))->find();

        $ReturnArray = array( // 返回字段
            "memberid" => $p["memberid"], // 商户ID
            "orderid" =>  $p["orderid"], // 订单号
            'transaction_id' => $p["transaction_id"],
            "amount" =>  $p["amount"], // 交易金额
            "datetime" =>  $p["datetime"], // 交易时间
            "returncode" => $p["returncode"]
        );

        $key = $this->config['signkey'];

        ksort($ReturnArray);

        $str = "";
        foreach ($ReturnArray as $k => $v) {
            $str .= $k."=".$v."&";
        }
        $str .= "key=".$key;

        $sign = $p['sign'];
        $sign2 = strtoupper(md5($str));

        if($sign != $sign2){
            exit("sign_wrong");
        }
        if($p['returncode'] != '00'){
            exit("pay_fail");
        }

        //处理支付
        //$this->EditMoney($find['pay_orderid'],"Yifa",0);

        //回调成功
        //echo 'ok'; exit;
        $this->async($find['pay_orderid'],'ok');
    }

    public function callBackUrl(){

        echo '处理成功'; exit;
    }
}